<?php
/**
 * 语言包管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: LanguageController.class.php v1.0 2014-06-23 17:27:03 $
 */
namespace Admin\Controller;

class LanguagesController extends CommonController{
    private $model;
    private $service;
    private $logic;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Language");
        $this->service = D("Language", "Service");
        $this->logic = D("Language", "Logic");
    }
    
    /**
     * 语言列表
     */
    public function index() {
        $result = $this->getPagination('Language');
        //var_dump($result);die;
        $this->assign('list', $result['data']);
        $this->assign('count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }
    
    /**
     * 添加语言
     */
    public function add() {
        $this->display();
    }
    
    /**
     * 创建语言
     * @return type
     */
    public function create() {
        $langs = I("post.lang", null);
        if(!isArrayAndNotEmpty($langs)){
            return $this->errorReturn("无效操作！");
        }
        // 检查数据是否合法
        $result = $this->service->checkLang($langs);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        // 添加数据
        $result = $this->service->add($langs);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        //生成新增语言包
        $this->logic->genLangFile($langs['nid']);
        $this->logic->genLangList();
        
        return $this->successReturn("添加语言成功！", U("index"));
    }
    
    /*
     * 修改
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->error("无效操作！");
        }
        $langs = $this->service->getById($id);
        if(!isArrayAndNotEmpty($langs)){
            return $this->error("您要编辑的语言不存在！");
        }
        $this->assign("lang", $langs);
        $this->display();
    }
    
    /**
     * 更新
     */
    public function update() {
        $langs = I("post.lang", null);
        if(!isArrayAndNotEmpty($langs)){
            return $this->errorReturn("无效操作！");
        }
        $langs_info = $this->service->getById($langs['id']);
        if(!isArrayAndNotEmpty($langs_info)){
            return $this->errorReturn("无效操作！");
        }
        // 检查数据是否合法
        $result = $this->service->checkLang($langs, $langs['id']);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        // 添加数据
        $result = $this->service->update($langs);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        
        $this->logic->genLangList();
        return $this->successReturn("修改语言成功！", U("index"));
    }
    
    /**
     * 删除语言
     * @return type
     */
    public function delete() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->errorReturn("无效操作！");
        }
        $langs_info = $this->service->getById($id);
        if(!isArrayAndNotEmpty($langs_info)){
            return $this->errorReturn("无效操作！");
        }
        if($langs_info["is_system"] == 1){
            return $this->errorReturn("系统默认语言禁止删除！");
        }
        if($this->service->delete($id)){
            //删除语言包
            $this->logic->delLangFile($langs_info['nid']);
            $this->logic->genLangList();
            return $this->successReturn("删除成功", U("index"));
        }else{
            return $this->errorReturn("删除失败！");
        }
        
    }
    
    /**
     * 语言包管理
     */
    public function language() {
        //echo LANG_PATH;
        $id = I("get.id", 0, "intval");
        $langs = $this->service->getById($id);
        if(!isArrayAndNotEmpty($langs)){
            return $this->error("非法操作！");
        }
        //var_dump($langs);die;
        $lang_file = LANG_PATH . $langs["nid"] . ".php";
        $lang_list = $this->service->getAll();
        $content = GetFileContent($lang_file);
        $this->assign($langs);
        $this->assign("lang_list", $lang_list);
        $this->assign("file_name", $lang_file);
        $this->assign("code", $content);
        $this->display();
    }
    
    /**
     * 切换模型状态
     * @return
     */
    public function toggleStatus() {
        $id = I("post.id", 0, "intval");
        $status = I("post.status", NULL);
        if (!isset($id)
            || !$this->service->existByIdLang($id)) {
            return $this->errorReturn('无效的操作！');
        }

        if ($status == 'yes') {
            $this->service->setStatus($id, 1);
        } else if($status === 'no'){
            $this->service->setStatus($id, 0);
        }else{
            return $this->errorReturn('无效的操作！');
        }
        $this->logic->genLangList();
        $info = $status === 'no' ? '禁用成功！' : '启用成功！';
        $this->successReturn($info);
    }
}

